# -*- coding: utf-8 -*-
"""
图像定位功能2
@author:   XueJunJie
@file:     ImageLocation2.py
@date:     2024-9-20
@version:  1.0
"""
import time
import pyscreeze


def png_location_element2(target_png: str, body_png: str = None, **kwargs):
    """
    :target_png        str                 目标图文件
    :body_png          str                 原始图文件,不填则默认全屏截图
    :grayscale         bool                灰度比较，默认False
    :region            [x,y,width,height]  范围
    :step              int                 步长,默认1，数值越大越不精准
    #自定义
    :farm               [(x1,y1),(x2,y2)]   同region范围含义,但接受的是坐标
    res:
        (int, int)   目标中心坐标，无目标则值为None
        [            目标区域
            (int, int)   起始坐标，无目标则值为None
            (int, int)   结尾坐标，无目标则值为None
        ]
    """
    t1 = time.time()
    farm = kwargs.pop('farm', None)
    if farm:
        kwargs['region'] = [
            farm[0][0],
            farm[0][1],
            farm[1][0] - farm[0][0],
            farm[1][1] - farm[0][1]
        ]
    if body_png:
        res = pyscreeze.locate(target_png, body_png, **kwargs)
    else:
        res = pyscreeze.locateOnScreen(target_png, **kwargs)
    if not res:
        print(f'耗时{round(time.time() - t1)}秒, 无结果')
        return None, None
    else:
        center = pyscreeze.center(res)
        farm = [
            (res.left, res.top),
            (res.left + res.width,  res.top + res.height)
        ]
        print(f'耗时{round(time.time() - t1)}秒, ({farm[0][0]},{farm[0][1]}),({farm[1][0]},{farm[1][1]})')
        return (center.x, center.y), farm
